package com.hxkj.service.userCenter.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.FieldTagServiceClient;
import com.hxkj.domain.po.FieldTag;
import com.hxkj.service.userCenter.FieldTagService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangheng
 * @date 2018-10-11 9:48
 */
@Service
@Slf4j
public class FieldTagServiceImpl implements FieldTagService {
    @Autowired
    private FieldTagServiceClient fieldTagServiceClient;

    @Override
    public PageInfo<FieldTag> getList(FieldTag fieldTag, PageTo pageParam) throws Exception {
        if(StringUtils.isBlank(fieldTag.getClassify())){
            throw new CustomMessageException("分类不能为空");
        }
        if(pageParam.getPageNum() == null || pageParam.getPageSize() == null){
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<FieldTag> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setPageTo(pageParam);
        parameterDataStruct.setData(fieldTag);
        ResultDataStruct<PageInfo<FieldTag>> result = fieldTagServiceClient.getList(parameterDataStruct);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void delete(Integer id) throws Exception {
        if(id == null){
            throw new CustomMessageException("编号不能为空");
        }
        ResultMessageStruct result = fieldTagServiceClient.delete(id);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void save(FieldTag fieldTag) throws Exception {
        if(fieldTag.getName() == null){
            throw new CustomMessageException("名称不能为空");
        }
        if(StringUtils.isBlank(fieldTag.getClassify())){
            throw new CustomMessageException("分类不能为空");
        }
        fieldTag.setType(0);
        ResultMessageStruct result = fieldTagServiceClient.save(fieldTag);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void update(FieldTag fieldTag) throws Exception {
        if(fieldTag.getId() == null){
            throw new CustomMessageException("编号不能为空");
        }
        if(StringUtils.isBlank(fieldTag.getClassify())){
            throw new CustomMessageException("分类不能为空");
        }
        ResultMessageStruct result = fieldTagServiceClient.update(fieldTag);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }
}
